define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'supplychain/index',
                    add_url: 'supplychain/add',
                    edit_url: 'supplychain/edit',
                    del_url: 'supplychain/del',
                    multi_url: 'supplychain/multi',
                    table: '',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
//当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'cid', title: '分类', visible: false, searchList:$.getJSON('supplychain/getCategory')},
                        {field: 'bid', title: '品牌', visible: false, searchList:[]},
                        {field: 'original_image', title:'图片', formatter: Table.api.formatter.image, operate: false},
                        {field: 'keyword', title:　'商品名称', visible:false},
                        {field: 'category', title:　'分类', visible:false},
                        {field: 'brand', title:　'品牌', visible:false},
                        {field: 'goods_no', title: '商品货号', sortable: true, operate: false},
                        {field: 'goods_name', title: '商品名称', operate: false},
                        {field: 'shop_price', title: '本店价', operate: false, sortable: true},
                        {field: 'market_price', title: '市场价', operate: false, sortable: true},
                        {field: 'goods_remark', title: '商品详情', operate: false, cellStyle: function () {return {css: {"max-width": "200px","overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}}},
                        {field: 'is_virtual', title: '虚拟商品', operate: false},
                        {field: 'is_free_shipping', title: '是否包邮', operate: false},
                        {field: 'sale_number', title: '销售量', operate: false, sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [{
                                name: 'spec',
                                text: '查看详情',
                                title: '查看详情',
                                icon: '',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: 'supplychain/spec?no={goods_no}',
                            }],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 监听下拉列表改变的事件
            $(document).on('change', 'select[name=cid]', function () {
                var cid = $(this).val();
                var str = '<option value="">请选择</option>';
                $.post('supplychain/getBrand', {cid: cid}, function(res) {
                    res.forEach(function(item, index){
                        str += '<option value="'+item.bid+'">'+item.name+'</option>';
                    });
                    $('select[name=bid]').html(str);
                },'json');
                
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        spec: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});